#include "../apue.h"
#include <sys/wait.h>

int main(void)
{
    pid_t pid1;
    pid_t pid2;
    pid_t pid3;

    time_t slp;

    if ((pid1 = fork()) < 0)
        err_sys("fork error");
    if (pid1 == 0)
    {
        slp = 3;
        sleep(slp);
        printf("pid1(%d) sleep %lds.\n", getpid(), slp);
        exit(0);
    }


    if ((pid2 = fork()) < 0)
        err_sys("fork error");
    if (pid2 == 0)
    {
        slp = 2;
        sleep(slp);
        printf("pid2(%d) sleep %lds.\n", getpid(), slp);
        exit(0);
    }


    if ((pid3 = fork()) < 0)
        err_sys("fork error");
    if (pid3 == 0)
    {
        slp = 1;
        sleep(slp);
        printf("pid3(%d) sleep %lds.\n", getpid(), slp);
        exit(0);
    }


    if (waitpid(pid1, NULL, 0) != pid1)
        err_sys("waitpid error");
    else
        printf("waitpid pid1(%d) end.\n", pid1);


    if (waitpid(pid2, NULL, 0) != pid2)
        err_sys("waitpid error");
    else
        printf("waitpid pid2(%d) end.\n", pid2);


    if (waitpid(pid3, NULL, 0) != pid3)
        err_sys("waitpid error");
    else
        printf("waitpid pid3(%d) end.\n", pid3);

    return 0;
}


